﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;

namespace Sinacor.OMM.Configuration.Dumper
{
    /// <summary>
    /// Read Dumper configuration
    /// </summary>
    public class DumperConfigurationReader : BaseResourceAccess
    {

        private Int32 _companyId;

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public DumperConfigurationReader(Int32 companyId)
            : base(companyId)
		{
            _companyId = companyId;
		}

		#endregion

        #region Methods

        /// <summary>
        /// Read the dumper configuration
        /// </summary>
        /// <returns></returns>
        public OMM.Configuration.Dumper.DumperConfiguration GetConfiguration()
        {
            //
            OMM.Configuration.Dumper.DumperConfiguration dumperConfiguration = null;
            Configuration.Repository.RepositoryConfigurationReader repositoryReader = new Sinacor.OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId);
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT COD_REPRIO_MSG,
                                   NOME_DRTO_PAI_ARQ 
                            FROM TBOCNFG_GERC_ARQ ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                dumperConfiguration = this.FindOne<OMM.Configuration.Dumper.DumperConfiguration>(dbCommand,
                    reader =>
                    {
                        OMM.Configuration.Dumper.DumperConfiguration item = new DumperConfiguration();

                        item.RootPath = reader.GetValue<String>("NOME_DRTO_PAI_ARQ");
                        item.MessageRepository.Id = reader.GetValue<Int32>("COD_REPRIO_MSG");

                        return item;
                    });
            }

            if (dumperConfiguration.MessageRepository.Id.HasValue)
                dumperConfiguration.MessageRepository = repositoryReader.GetConfiguration(dumperConfiguration.MessageRepository.Id.Value);
            else
                dumperConfiguration.MessageRepository = null;

            return dumperConfiguration;
        }

        #endregion

    }
}
